package com.oscar.ehome.view.pageview.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.oscar.ehome.R;
import com.oscar.ehome.bean.pagebean.ApplationsBean;
import com.oscar.ehome.bean.pagebean.PageAppBean;
import com.oscar.ehome.utils.system.SystemConfigFactory;

import net.tycmc.bulb.androidstandard.utils.JsonUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/6/28
 * 删除view  adapter
 */

public class DeleteGridAdapter extends BaseAdapter {

    private List<String> specailList = new ArrayList<>();
    private LayoutInflater mInflater;
    private Context mContext = null;
    DeleteClicklister onClickListener;
    private int hidePosition = AdapterView.INVALID_POSITION;
    int colnum = 1;
    //是否展示删减图标
    int update = 0;

    public DeleteGridAdapter(Context context, int colnum, List<String> specailList) {
        this.mContext = context;

        this.specailList = specailList;
        this.colnum = colnum;
        mInflater = LayoutInflater.from(context);
    }

    public void setList(List<String> list, int colnum) {
        this.specailList = list;
        this.colnum = colnum;
        notifyDataSetChanged();
    }

    /**
     * 设置回调接口
     *
     * @param onClickListener
     */
    public void setDeleteClicksListener(DeleteClicklister onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface DeleteClicklister {
        public void DeleteClick(int postion);
    }

    public void setUpdate(int update) {
        this.update = update;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (specailList.size() < colnum) {
            return specailList.size();
        }
        return colnum;
    }

    @Override
    public Object getItem(int position) {
        return specailList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_deletegridview, null);
            holder = new ViewHolder();
            holder.delete_text = (TextView) convertView.findViewById(R.id.delete_text);
            holder.delete_gridlayout = (RelativeLayout) convertView.findViewById(R.id.delete_gridlayout);
            holder.delete_img = (ImageView) convertView.findViewById(R.id.delete_img);
            holder.delete_gridimg = (ImageView) convertView.findViewById(R.id.delete_gridimg);
            convertView.setTag(holder); //
        } else {
            holder = (ViewHolder) convertView.getTag(); //
        }
        if (update == 1) {
            Resources res = mContext.getResources();
            Drawable draw = res.getDrawable(R.drawable.bianjibiankuang);
            //设置linearLayout的背景图，将Drawable对象放进去
            holder.delete_gridlayout.setBackground(draw);
            //holder.delete_gridlayout.setBackgroundColor(Color.parseColor("#f2f2f2"));
            holder.delete_gridimg.setVisibility(View.VISIBLE);
        } else {
            holder.delete_gridimg.setVisibility(View.GONE);
            holder.delete_gridlayout.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        if (specailList.get(position) != null && !"".equals(specailList.get(position))) {
            //hide时隐藏Text
            if (position != hidePosition) {
                holder.delete_gridlayout.setVisibility(View.VISIBLE);
                String stats = specailList.get(position);
                switch (stats) {
                    case "8"://应用中心
                        holder.delete_text.setText(R.string.yyzx);
                        holder.delete_img.setImageResource(R.mipmap.yyzx);
                        break;
                    case "7"://考勤签到
                        holder.delete_text.setText(R.string.kqqd);
                        holder.delete_img.setImageResource(R.mipmap.indexqian);
                        break;
                    case "6"://统计
                        holder.delete_text.setText(R.string.tj);
                        holder.delete_img.setImageResource(R.mipmap.indextongji);
                        break;
                    case "9"://准会员
                        holder.delete_text.setText(R.string.zhy);
                        holder.delete_img.setImageResource(R.mipmap.huiyuan);
                        break;
                    case "4"://学习管理
                        holder.delete_text.setText(R.string.study);
                        holder.delete_img.setImageResource(R.mipmap.xuexi);
                        break;
                    case "3"://活动管理
                        holder.delete_text.setText(R.string.hd);
                        holder.delete_img.setImageResource(R.mipmap.huodong);
                        break;
                    case "11"://寿险通
                        holder.delete_text.setText(R.string.sxt);
                        holder.delete_img.setImageResource(R.mipmap.sxt);
                        break;
                    case "10"://掌中宝
                        holder.delete_text.setText(R.string.zzb);
                        holder.delete_img.setImageResource(R.mipmap.zzb);
                        break;
                    case "12"://保险档案
                        holder.delete_img.setImageResource(R.mipmap.dangan1);
                        holder.delete_text.setText(R.string.insurance_text);
                        break;
                    case "13"://工作日志
                        holder.delete_img.setImageResource(R.mipmap.jilu);
                        holder.delete_text.setText(R.string.log_text);
                        break;
                    case "14"://工作日志
                        holder.delete_img.setImageResource(R.mipmap.zhagnzhishi);
                        holder.delete_text.setText(R.string.know_text);
                        break;
                    case "15"://获客
                        holder.delete_img.setImageResource(R.mipmap.huoke);
                        holder.delete_text.setText(R.string.huoke);
                        break;
                    case "16"://考勤签到
                        holder.delete_text.setText(R.string.kqqd);
                        holder.delete_img.setImageResource(R.mipmap.indexqian);
                        break;
                    case "17"://督导
                        holder.delete_text.setText(R.string.dudao);
                        holder.delete_img.setImageResource(R.mipmap.dudaowang);
                        break;
                    case "18"://督导
                        holder.delete_text.setText(R.string.nimingjubao);
                        holder.delete_img.setImageResource(R.mipmap.nimingjubao);
                        break;
                    case "19"://nimingjubao
                        holder.delete_img.setImageResource(R.mipmap.mingpian);
                        holder.delete_text.setText(R.string.yaoqinghan);
                        break;
                    case "20"://nimingjubao
                        holder.delete_img.setImageResource(R.mipmap.youxi);
                        holder.delete_text.setText(R.string.youxi);
                        break;
                    case "21":
                        holder.delete_img.setImageResource(R.mipmap.tuoguan);
                        holder.delete_text.setText(R.string.baodantuoguan);
                        break;
                    case "22":
                        holder.delete_img.setImageResource(R.mipmap.yejichaxun);
                        holder.delete_text.setText(R.string.yejichaxun);
                        break;
                }
            } else {
                holder.delete_gridlayout.setVisibility(View.GONE);
            }
        }

        holder.delete_gridimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onClickListener.DeleteClick(position);

            }
        });

        return convertView;
    }

    public void hideView(int pos) {
        hidePosition = pos;
        notifyDataSetChanged();
    }

    public void showHideView() {
        hidePosition = AdapterView.INVALID_POSITION;
        notifyDataSetChanged();
    }

    public void removeView(int pos) {
        specailList.remove(pos);
        notifyDataSetChanged();
    }

    //更新拖动时的gridView
    public void swapView(int draggedPos, int destPos) {
        //从前向后拖动，其他item依次前移
        if (draggedPos < destPos) {
            specailList.add(destPos + 1, (String) getItem(draggedPos));
            specailList.remove(draggedPos);
        }
        //从后向前拖动，其他item依次后移
        else if (draggedPos > destPos) {
            specailList.add(destPos, (String) getItem(draggedPos));
            specailList.remove(draggedPos + 1);
        }
        hidePosition = destPos;
        save();
        notifyDataSetChanged();
    }

    /**
     * 本地存储一份list
     */
    public void save() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("deletelist", specailList);
        String app = JsonUtils.toJson(map);
        SystemConfigFactory.getInstance(mContext).getSystemConfig().setDeleteapp(app);
    }

    class ViewHolder {
        TextView delete_text;
        RelativeLayout delete_gridlayout;
        ImageView delete_img;
        ImageView delete_gridimg;//删除按钮
    }

}
